/*
This file is part of Cafca.

Cafca is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Cafca is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Cafca.  If not, see <http://www.gnu.org/licenses/>.

If you have any comment, suggestion or idea, do not hesitate to contact me at shenrotin@gmail.com
*/

package org.crag.cafca.gui;

import java.util.Collection;
import java.util.Vector;

import org.apache.log4j.Logger;

public class Submenu 
{
	/**
	 * Logger log4j. See log4j doc for more information.
	 */
	private static Logger log=Logger.getLogger(new Throwable().getStackTrace()[0].getClassName());
		
	protected String name;
	protected String id;
	
	protected Vector<Submenu> subsystems;
	protected Submenu parent;
	private String url;
	
	public Submenu()
	{
		this.subsystems = new Vector<Submenu>();
	}
	
	public Submenu findChild(String id)
	{
		for (Submenu subsystem:subsystems)
		{
			if (subsystem.getId().equals(id))
				return subsystem;
		}
		
		return null;
	}

	public String getFullId()
	{
		StringBuffer result=new StringBuffer();
		Submenu currentSubsystem=this;
		while (currentSubsystem!=null)
		{
			result.insert(0,"/"+currentSubsystem.getId());
			currentSubsystem=currentSubsystem.getParent();
		}
		
		result.deleteCharAt(0);
		
		return result.toString();
	}
	
	public String getId() 
	{
		return id;
	}

	public void setId(String id)
	{
		this.id=id;
	}
	
	public void addSubmenu(Submenu submenu)
	{
		submenu.setParent(this);
		this.subsystems.add(submenu);
	}
	

	public void removeSubSystem(Submenu subsystem)
	{
		subsystem.setParent(null);
		this.subsystems.remove(subsystem);
	}

	private void setParent(Submenu parent) 
	{
		this.parent=parent;
	}

	public void removeSubSystem(String id)
	{
		this.removeSubSystem(findChild(id));
	}
	
	public String getName() 
	{
		return name;
	}

	public Vector<Submenu> getSubmenus() 
	{
		return subsystems;
	}
	
	public Submenu getParent()
	{
		return parent;
	}

	public void setName(String name) 
	{
		this.name=name;
	}

	public void setUrl(String url) 
	{
		this.url=url;
	}

	public boolean isLeaf() 
	{
		return (subsystems.size()==0);
	}

	public String getUrl() 
	{
		return url;
	}
}
